package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.AclAdmin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.FileUtil;
import com.atguigu.util.QiniuUtils;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;
    private final static String PAGE_INDEX = "admin/index";
    private static final String PAGE_CREATE = "admin/create";
    private final static String LIST_ACTION = "redirect:/admin";
    private final static String PAGE_EDIT = "admin/edit";
    @RequestMapping
    public String index(@RequestParam Map filters, Model model){
        PageInfo<AclAdmin> pageInfo = adminService.findPage(filters);
        model.addAttribute("page",pageInfo);
        model.addAttribute("filters",filters);
        return PAGE_INDEX;
    }


    @RequestMapping("/create")
    public String create(){
        return PAGE_CREATE;
    }

    @Autowired
    private PasswordEncoder passwordEncoder;
    @PostMapping("/save")
    public String save(AclAdmin admin, Model model){
        if ("admin" == admin.getUsername()) {
            return successPage(model,"不允许创建admin用户");
        }
        //设置密码
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return successPage(model,"新增用户成功");
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model){
        AclAdmin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return PAGE_EDIT;
    }

    @PostMapping("/update")
    public String update(AclAdmin admin,Model model){
        adminService.update(admin);
        return successPage(model,"编辑用户成功");
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        adminService.delete(id);

        return LIST_ACTION;
    }

    private final static String PAGE_UPLOAD_SHOW = "admin/upload";

    @GetMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable("id") Long id,Model model){
        model.addAttribute("id",id);

        return PAGE_UPLOAD_SHOW;
    }

    @PostMapping("/upload/{id}")
    public String upload(@PathVariable("id") Long id, Model model,@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        String uuidName = FileUtil.getUUIDName(originalFilename);
        QiniuUtils.upload2Qiniu(multipartFile.getBytes(),uuidName);

        AclAdmin admin = new AclAdmin();
        admin.setId(id);
        admin.setHeadUrl("http://sold6uxhm.hn-bkt.clouddn.com/"+uuidName);

        adminService.update(admin);

        return successPage(model,"头像上传成功");
    }

    private static final String PAGE_ASSIGN_SHOW = "admin/assignShow";
    @Reference
    private RoleService roleService;
    @GetMapping("/assignShow/{id}")
    public String assignShow(@PathVariable Long id,Model model){
        model.addAttribute("adminId",id);
        //unAssignRoleList 未分配的角色
        //assignRoleList 已分配的角色
        Map<String,Object> map = roleService.findRoleListByAdminId(id);
        //将map中的unAssignRoleList,assignRoleList放入到request域
        model.addAllAttributes(map);

        return PAGE_ASSIGN_SHOW;
    }


    @PostMapping("/assignRole")
    public String assignRole(@RequestParam Long adminId, @RequestParam Long[] roleIds, Model model){
        //这里这么用方法,只是为了把参数传递到后面进行处理
        adminService.assignRole(adminId,roleIds);
        return successPage(model,"保存角色成功");
    }
}
